package com.hjs.servlet.regularinspection.pavementdamage;

import com.hjs.bean.PavementDamage;
import com.hjs.bean.PavementDamageCom;
import com.hjs.mapper.RegularInspectionMapper;
import com.hjs.tools.Mapper;
import com.hjs.tools.RequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/PavementDamage/update")
@Slf4j
public class UpdatePavementDamage extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        RequestUtil requestUtil = new RequestUtil(request, response);
        SqlSession sqlSession = Mapper.getSqlSession();
        boolean sqlResult = updatePavementDamageCom(requestUtil, sqlSession);
        if (sqlResult) {
            sqlResult = updatePavementDamage(requestUtil, sqlSession);
        }
        requestUtil.responseIsSuccess(sqlResult,"更新失败");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        doPost(request, response);
    }

    private boolean updatePavementDamageCom(RequestUtil requestUtil, SqlSession sqlSession) {
        /*
         * 提供参数
         * pavementDamageComID
         * inspectionDate
         * inspectorID
         * SAEPlace
         * totalLength
         * totalWidth
         * regularInspectionID
         */
        try(sqlSession) {
            PavementDamageCom pavementDamageCom = new PavementDamageCom(requestUtil.getInt("pavementDamageComID"),
                    requestUtil.getDate("inspectionDate"), requestUtil.getString("inspectorID"),
                    requestUtil.getString("SAEPlace"), requestUtil.getDouble("totalLength"),
                    requestUtil.getDouble("totalWidth"),
                    requestUtil.getDouble("totalLength") * requestUtil.getDouble("totalWidth"),
                    requestUtil.getInt("regularInspectionID"));
            RegularInspectionMapper mapper = sqlSession.getMapper(RegularInspectionMapper.class);
            boolean sqlResult = mapper.updateOnePavementDamageCom(pavementDamageCom);
            Mapper.SessionEnd(sqlResult, sqlSession, "更新路面损坏情况失败");
            return sqlResult;
        } catch (Exception e) {
            log.error(e.toString());
            return false;
        }
    }

    private boolean updatePavementDamage(RequestUtil requestUtil, SqlSession sqlSession) {
        /*
         * 提供参数
         * pavementDamageID
         * pavementDamageComID
         * damageTypeID
         * damageLength
         * damageWidth
         * damageHeight
         * damagePAC
         * remarks（可选）
         */
        try (sqlSession) {
            RegularInspectionMapper mapper = sqlSession.getMapper(RegularInspectionMapper.class);
            PavementDamageCom pavementDamageCom = mapper.getPavementDamageComById(requestUtil.getInt("pavementDamageComID"));
            String[] properties = {"damageTypeID", "damageLength", "damageWidth", "damageHeight", "damagePAC", "remarks"};
            boolean sqlResult = mapper.updateOnePavementDamage(new PavementDamage(
                    requestUtil.getInt("pavementDamageID"), pavementDamageCom, requestUtil.getInt(properties[0]),
                    requestUtil.getDouble(properties[1]), requestUtil.getDouble(properties[2]),
                    requestUtil.getDouble(properties[3]), requestUtil.getString(properties[4]),
                    requestUtil.getString(properties[5])));
            Mapper.SessionEnd(sqlResult, sqlSession, "更新路面损坏情况细则失败");
            return sqlResult;
        } catch (Exception e) {
            log.error(e.toString());
            return false;
        }
    }
}
